package Top4.confirm;

import com.rabbitmq.client.*;
import util.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @Auther: Administrator
 * @Date: 2019/3/5 20:50
 * @Description:
 */
public class ConsumerConfirm {
    private static final String QUEUE_NAME = "queue_confirm";
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Connection connection = ConnectionUtil.getConnection();

         final   Channel channel = connection.createChannel();

        channel.basicQos(64); // 设置客户端最多接收未被ack的消息的个数
        final Consumer consumer = new DefaultConsumer(channel) {

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
                    throws IOException {
                System.out.println(" recv message: "  + new String(body));
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };



        //autoAck设置为false 可以防止消息不必要的丢失   建议设置为false
        channel.basicConsume(QUEUE_NAME, false, consumer);

        TimeUnit.SECONDS.sleep(50000);
        channel.close();
        connection.close();
    }
}
